package com.freesky.biz.icbc.service;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.freesky.biz.icbc.config.IcbcConfiguration;
import com.freesky.biz.icbc.model.IcbcEquityQryRequestV1;
import com.freesky.biz.icbc.model.IcbcEquityQryResponseV1;
import com.freesky.common.core.domain.AjaxResult;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.IcbcConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import java.util.*;

//权益平台对接接口服务
@Service
public class IcbcEquityService {

    private static Logger logger = LoggerFactory.getLogger(IcbcEquityService.class);

    @Autowired
    @Qualifier("icbcConfiguration")
    private IcbcConfiguration icbcConfiguration;

    private String PREFIX_URL = "https://gw.open.icbc.com.cn/api/";

    //测试环境
    //private String PREFIX_URL = "https://apipcs3.dccnet.com.cn/api/";

    private String MCT_CODE = "0002";
    private String DEEPARTMENT = "YHKZX";   //YHKZX，GRJRYWB（个金金融部），YHKZX，YHKZX


    //客户权益查询（1201）
    public AjaxResult equityQry(String mobile){
        AjaxResult resultModel = AjaxResult.success();
        try{
            DefaultIcbcClient client = new DefaultIcbcClient(icbcConfiguration.getAppId(),
                    IcbcConstants.SIGN_TYPE_RSA2,
                    icbcConfiguration.getPrivateKey(),
                    icbcConfiguration.getPubGatewayKey());
            IcbcEquityQryRequestV1 request = new IcbcEquityQryRequestV1();
            request.setServiceUrl(PREFIX_URL + "fjfh/mybank/account/stqypt/equityqry/V1");

            IcbcEquityQryRequestV1.IcbcEquityQryRequestV1Biz bizContent = new IcbcEquityQryRequestV1.IcbcEquityQryRequestV1Biz();

            bizContent.setMti("1201");
            bizContent.setMctcode(MCT_CODE);
            bizContent.setOpenId("");
            bizContent.setMobileNo(mobile);
            bizContent.setTrantime(DateUtil.format(new Date(),"yyyyMMddHHmmss"));
            request.setBizContent(bizContent);

            logger.info("【equityQry req,mobile:"+mobile+"】:"+ JSON.toJSONString(bizContent)+",url:"+request.getServiceUrl());
            IcbcEquityQryResponseV1 response = client.execute(request);
            logger.info("【equityQry resp,mobile:"+mobile+"】:"+JSON.toJSONString(response));
            if (response.isSuccess()) {
                Map<String,Object> respMap = new HashMap<>();
                respMap.put("applyFlag",response.getApplyFlag());
                Map<String, IcbcEquityQryResponseV1.IcbcEquityQryResponseV1Body> mapActivity = new HashMap<>();
                if("0".equalsIgnoreCase(response.getApplyFlag())){
                    JSONArray jsonArray = JSON.parseArray(response.getBody());
                    for(int i=0;i<jsonArray.size();i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        IcbcEquityQryResponseV1.IcbcEquityQryResponseV1Body responseV1Body = new IcbcEquityQryResponseV1.IcbcEquityQryResponseV1Body();
                        responseV1Body.setOrderNo(jsonObject.getString("orderno"));
                        responseV1Body.setActivityId(jsonObject.getString("activityid"));
                        responseV1Body.setCardcoopcode(jsonObject.getString("cardcoopcode"));
                        responseV1Body.setActivityLevel(jsonObject.getString("activitylevel"));
                        mapActivity.put(responseV1Body.getActivityId(),responseV1Body);
                    }
                }
                respMap.put("activityMap",mapActivity);

                resultModel.put("body",respMap);

            } else {
                return AjaxResult.error("操作失败，系统接口异常!");
            }

        } catch (Exception ioe){
            ioe.printStackTrace();
            return AjaxResult.error("操作失败，系统异常!");
        }
        return resultModel;
    }

 }
